package com.example.leetdemo.process;

public class Solution707 {

    class MyLinkedList {

        class Node {
            int value;
            Node next;
            Node first;

            public Node(int value) {
                this.value = value;
            }

            public Node(int value, Node next, Node first) {
                this.first = first;
                this.value = value;
                this.next = next;
            }
        }

        Node first;

        Node end;

        int count;

        public MyLinkedList() {
            this.count = 0;
        }

        public int get(int index) {
            if (index > count - 1 || index < 0) {
                return -1;
            }
            Node temp = this.first;
            int value = 0;
            while (temp != null) {
                if (value == index) {
                    return temp.value;
                }
                temp = temp.next;
            }
            return -1;
        }

        public void addAtHead(int val) {
            if (count == 0) {
                this.first = new Node(val);
                this.end = first;
            } else {
                Node newFirst = new Node(val, null, first);
                this.first.first = newFirst;
                this.first = newFirst;
            }
            count++;
        }

        public void addAtTail(int val) {
            if (count == 0) {
                this.first = new Node(val);
                this.end = first;
            } else {
                Node temp = new Node(val, this.end, null);
                this.end.next = temp;
                this.end = temp;
            }
            count++;
        }

        public void addAtIndex(int index, int val) {
            if (index <= 0) {
                addAtHead(val);
            } else if (index < count) {
                int i = 1;
                Node temp = this.first;
                while (temp != null) {
                    if (i == index) {
                        if (temp.next != null) {
                            Node node = new Node(val, temp, temp.next);
                            temp.next = node;
                            node.next.first = node;
                        } else {
                            addAtTail(val);
                        }
                        count++;
                        break;
                    } else {
                        i++;
                        temp = temp.next;
                    }
                }
            }
        }

        public void deleteAtIndex(int index) {

        }
    }
}
